package com.akorena.app.bridge;
import java.io.IOException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import ymsg.network.AccountLockedException;
import ymsg.network.LoginRefusedException;
import ymsg.network.Session;
import ymsg.network.event.SessionAuthorizationEvent;
import ymsg.network.event.SessionEvent;
import ymsg.support.MessageDecoder;
import ymsg.support.MessageDecoderSettings;
import ymsg.support.MessageElement;

public class YahooInterface {
	private static final Log log = LogFactory.getLog(YahooInterface.class);

	private Session yahooMessengerSession = null;
	private boolean loggedIn = false;
	private YahooSessionListener mySessionListener;
	
	private String server;
	private String username;
	private String password;
	private String status;
	
	public YahooInterface(String server, String username, String password, String status) {
		this.server = server;
		this.username = username;
		this.password = password;
		this.status = status;
	}

	public void logout() {

		if (loggedIn == false)
			return;

		try {
			yahooMessengerSession.logout();
		} catch (IllegalStateException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		loggedIn = false;	
	}

	public boolean login() {
		if (loggedIn == false) {
			
			yahooMessengerSession = new Session();

			mySessionListener = new YahooSessionListener(this);
			yahooMessengerSession.addSessionListener(mySessionListener);

			try {
				yahooMessengerSession.login(username, password);
				if (status != null) yahooMessengerSession.setStatus(status, false);
				log.info("Successfully connected");
				loggedIn = true;
			} catch (IllegalStateException e1) {
				log.info("Login Failed: " + e1.getMessage());
				// e1.printStackTrace();
				loggedIn = false;
			} catch (AccountLockedException e1) {
				loggedIn = false;
				log.info("Login Failed: " + e1.getMessage());
				// e1.printStackTrace();
			} catch (LoginRefusedException e1) {
				log.info("Login Failed: " + e1.getMessage());
				loggedIn = false;
				// e1.printStackTrace();
			} catch (UnknownHostException e1) {
				log.info("Login Failed: " + e1.getMessage());
				loggedIn = false;
			} catch (IOException e1) {
				loggedIn = false;
				log.info("Login Failed: " + e1.getMessage());
				// TODO Auto-generated catch block
				// e1.printStackTrace();

			}
		} else {
			log.info("Attempting logout");
			logout();
		}

		return loggedIn;
	}

	public void sendMessage(String to, String text) {
		try {
			yahooMessengerSession.sendMessage(to, text);
		} catch (IllegalStateException e1) {
			log.info("IllegalStateException: " + e1.getMessage());
			// TODO Auto-generated catch block
			// e1.printStackTrace();
		} catch (IOException e1) {
			log.info("IOException: " + e1.getMessage());
			// TODO Auto-generated catch block
			// e1.printStackTrace();
		}
	}

	public void contactRequestReceived(String friend) {
		try {
			yahooMessengerSession.addFriend(friend, "Friends");
		} catch (IllegalStateException e1) {
			log.info("IllegalStateException: " + e1.getMessage());
			// TODO Auto-generated catch block
			// e1.printStackTrace();
		} catch (IOException e1) {
			log.info("IOException: " + e1.getMessage());
			// TODO Auto-generated catch block
			// e1.printStackTrace();
		}
	}

	public void connectionClosed() {
		log.info("Trying to re-connect");
		loggedIn = false;
		login();
	}

	public void messageReceived(SessionEvent ev) {
		String message = ev.getMessage();
		String response = "";

		MessageDecoderSettings mds = new MessageDecoderSettings();
		mds.setEmoticonsDecoded(false);
		MessageDecoder md = new MessageDecoder(mds);
		MessageElement sc = md.decode(message);

		message = sc.toText();

		log.info(ev.getFrom() + " > " + message);
		
		
		// FIXME
		// send message
		List<NameValuePair> nvps = new ArrayList<NameValuePair>();
		nvps.add(new BasicNameValuePair("eventSource", ev.getFrom()));
		nvps.add(new BasicNameValuePair("message", message));
		
		response = HttpClient.post(server, nvps);
		
		log.info(ev.getFrom() + " < " + response);

		this.sendMessage(ev.getFrom(), response);
	}

	public void authorizationReceived(SessionAuthorizationEvent ev) {
		try {
			yahooMessengerSession.acceptFriendAuthorization(ev, "Friends");
			log.info(ev.getFrom() + " authorized");
		} catch (IllegalStateException e1) {
			log.info("IllegalStateException: " + e1.getMessage());
			// TODO Auto-generated catch block
			// e1.printStackTrace();
		} catch (IOException e1) {
			log.info("IOException: " + e1.getMessage());
			// TODO Auto-generated catch block
			// e1.printStackTrace();
		}	}
}
